package com.zmj.sy.mom.srv.aps.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.dto.phaseGroup.PhaseGroupDto;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.PhaseGroup;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.phaseGroup.PhaseGroupListReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.phaseGroup.PhaseGroupSelectReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.phaseGroup.PhaseGroupSelectResVo;
import com.zmj.sy.mom.srv.aps.mapper.PhaseGroupMapper;
import com.zmj.sy.mom.srv.aps.utils.MkBeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class PhaseGroupService {

    private final PhaseGroupMapper phaseGroupMapper;


    /**
     * @Description:  新增工序工作组
     * @author: lyh
     * @Param:
     * @date: 2024/10/28 11:02
     */
    @Transactional
    public void add(PhaseGroupDto dto) {
        PhaseGroup phaseGroup = MkBeanUtils.copyProperties(dto, PhaseGroup.class);
        phaseGroupMapper.insert(phaseGroup);
    }


    /**
     * @Description:  修改工序工作组
     * @author: lyh
     * @Param:
     * @date: 2024/10/28 11:02
     */
    @Transactional
    public void edit(PhaseGroupDto dto) {
        PhaseGroup phaseGroup = MkBeanUtils.copyProperties(dto, PhaseGroup.class);
        phaseGroupMapper.updateById(phaseGroup);
    }





    /**
     * @Description:  删除工序工作组
     * @author: lyh
     * @Param:
     * @date: 2024/10/28 11:02
     */
    @Transactional
    public void delete(BaseIdReqVo reqVo) {
        phaseGroupMapper.deleteById(reqVo.getId());
    }




    /**
     * @Description:  列表工序工作组
     * @author: lyh
     * @Param:
     * @date: 2024/10/28 11:03
     */
    public BasePageResVo<PhaseGroupDto> list(PhaseGroupListReqVo reqVo) {
        LambdaQueryWrapper<PhaseGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(reqVo.getGroupCode()), PhaseGroup::getGroupCode, reqVo.getGroupCode());
        wrapper.like(StringUtils.hasText(reqVo.getPhaseCode()), PhaseGroup::getPhaseCode, reqVo.getPhaseCode());
        wrapper.eq(PhaseGroup::getDeleted, 0);
        Page<PhaseGroup> page = phaseGroupMapper.selectPage(reqVo.toPage(), wrapper);
        return BasePageResVo.of(page, PhaseGroupDto.class);
    }

    public BaseListResVo<PhaseGroupSelectResVo> select(PhaseGroupSelectReqVo reqVo) {
        List<PhaseGroup> list = phaseGroupMapper.lambdaQuery()
                .in(!CollUtil.isEmpty(reqVo.getPhaseCode()), PhaseGroup::getPhaseCode, reqVo.getPhaseCode())
                .list();

        List<PhaseGroupSelectResVo> collect = list.stream()
                .map(e -> MkBeanUtils.copyProperties(e, PhaseGroupSelectResVo.class))
                .collect(Collectors.toList());

        return new BaseListResVo<>(collect);
    }

}
